/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_ssap_server_callback_stub.h"
#include "sle_hilog.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
SleSsapServerCallbackStub::SleSsapServerCallbackStub()
{
    memberFuncMap_[SleSsapServerCallbackInterfaceCode::SLE_SSPA_SERVER_CALLBACK_START_SERVICE] =
        &SleSsapServerCallbackStub::OnStartServiceInner;
    memberFuncMap_[SleSsapServerCallbackInterfaceCode::SLE_SSPA_SERVER_CALLBACK_DELETE_ALL_SERVICES] =
        &SleSsapServerCallbackStub::OnAllServiceDeleteInner;
    memberFuncMap_[SleSsapServerCallbackInterfaceCode::SLE_SSPA_SERVER_CALLBACK_READ_REQUEST] =
        &SleSsapServerCallbackStub::OnReadRequestInner;
    memberFuncMap_[SleSsapServerCallbackInterfaceCode::SLE_SSPA_SERVER_CALLBACK_WRITE_REQUEST] =
        &SleSsapServerCallbackStub::OnWriteRequestInner;
    memberFuncMap_[SleSsapServerCallbackInterfaceCode::SLE_SSPA_SERVER_CALLBACK_MTU_INFO_CHANGED] =
        &SleSsapServerCallbackStub::OnMtuInfoChangedInner;
}

SleSsapServerCallbackStub::~SleSsapServerCallbackStub(){}

int SleSsapServerCallbackStub::OnRemoteRequest(
    uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    if(code != SLE_SSPA_SERVER_CALLBACK_WRITE_REQUEST) {
        SLE_LOGD("cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
    }
    std::u16string descriptor = SleSsapServerCallbackStub::GetDescriptor();
    std::u16string remoteDescriptor = data.ReadInterfaceToken();
    SLE_ASSERT_AND_RETURN_RET_LOG(descriptor == remoteDescriptor, ERR_INVALID_STATE, "local descriptor is not equal to remote error!");

    auto itFunc = memberFuncMap_.find((SleSsapServerCallbackInterfaceCode)code);
    if (itFunc != memberFuncMap_.end()) {
        auto memberFunc = itFunc->second;
        if (memberFunc != nullptr) {
            return (this->*memberFunc)(data, reply);
        }
    }
    SLE_LOGW("default case, need check.");
    return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}

int SleSsapServerCallbackStub::OnStartServiceInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("OnStartService In");
    uint8_t serverId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(serverId), TRANSACTION_ERR, "read server id failed!");

    uint16_t handle;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(handle), TRANSACTION_ERR, "read service handle failed!");

    uint32_t errCode;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(errCode), TRANSACTION_ERR, "read result status failed!");

    OnStartService(serverId, handle, errCode);
    return NO_ERROR;
}

int SleSsapServerCallbackStub::OnAllServiceDeleteInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("OnAllServiceDeleteInner In");
    uint8_t serverId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(serverId), TRANSACTION_ERR, "read server id failed!");

    uint32_t errCode;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(errCode), TRANSACTION_ERR, "read result status failed!");

    OnAllServiceDelete(serverId, errCode);
    return NO_ERROR;
}

int SleSsapServerCallbackStub::OnReadRequestInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("OnReadRequestInner In");
    uint8_t serverId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(serverId), TRANSACTION_ERR, "read server id failed!");

    uint16_t connectId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(connectId), TRANSACTION_ERR, "read connect id failed!");

    std::shared_ptr<SsapServerReqReadCbParam> param(data.ReadParcelable<SsapServerReqReadCbParam>());
    SLE_ASSERT_AND_RETURN_RET_LOG(param, TRANSACTION_ERR, "read read request param failed!");

    uint32_t errCode;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(errCode), TRANSACTION_ERR, "read result status failed!");

    OnReadRequest(serverId, connectId, *param, errCode);
    return NO_ERROR;
}

int SleSsapServerCallbackStub::OnWriteRequestInner(MessageParcel &data, MessageParcel &reply)
{
#if 0
    SLE_LOGD("OnWriteRequestInner In");
#endif
    uint8_t serverId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(serverId), TRANSACTION_ERR, "read server id failed!");

    uint16_t connectId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(connectId), TRANSACTION_ERR, "read connect id failed!");

    std::shared_ptr<SsapServerReqWriteCbParam> param(data.ReadParcelable<SsapServerReqWriteCbParam>());
    SLE_ASSERT_AND_RETURN_RET_LOG(param, TRANSACTION_ERR, "read write request param failed!");

    uint32_t errCode;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(errCode), TRANSACTION_ERR, "read result status failed!");

    OnWriteRequest(serverId, connectId, *param, errCode);
    return NO_ERROR;
}

int SleSsapServerCallbackStub::OnMtuInfoChangedInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("OnMtuInfoChangedInner In");
    uint8_t serverId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(serverId), TRANSACTION_ERR, "read server id failed!");

    uint16_t connectId;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(connectId), TRANSACTION_ERR, "read connect id failed!");

    uint32_t mtu;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(mtu), TRANSACTION_ERR, "read mtu failed!");

    uint16_t version;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint16(version), TRANSACTION_ERR, "read version failed!");

    uint32_t errCode;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint32(errCode), TRANSACTION_ERR, "read result status failed!");

    OnMtuInfoChanged(serverId, connectId, mtu, version, errCode);
    return NO_ERROR;
}
} // SleStandard
} // NearLink
} // OHOS